const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
      
        startImg:'https://image.browin.net.cn/202301/coupon.png',
        size:750,
        transform:30,
        error:{300:'内部员工不允许抽券',400:'您已经有一张券',500:'您的券未核销或者您没使用',600:'您已经抽过奖了',700:'您已经抽过奖了'}
       
    },
    onLoad(options){
        let that=this;
        console.log(options);
        var type=options.type;
        var parent_id=options.parent_id;  
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })        
        that.setData({
            type:type,
            parent_id:parent_id,
            platform:app.globalData.platform,    
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain
          })
       
    },
    getCoupon:function (){
        let that=this;
        var userInfo=wx.getStorageSync('userInfo');
        wx.request({
            url: app.globalData.domain+'lottery/getCoupon',
            data:{from:1,user_id:userInfo.id},
            success:res=>{
                //console.log(res);
                if(res.statusCode==200){
                    let data=res.data;
                    that.setData({
                        prizeList:data.data,
                        backgroud:data.backgroud,
                      
                        list:res.data.list,
                        shareTitle:data.shareTitle,
                        shareLogo:data.shareLogo,
                        startImg:data.startImg
                    })
                    that.initTurntable();
                }
            }
        })
    },

    onShow(){
  
        var type=this.data.type;
         this.getCoupon();
        
      
    },
    initTurntable: function() {
        var that = this;
        const query = that.createSelectorQuery()
        query.select('#canvas')
          .fields({ node: true, size: true })
          .exec((res) => {
            console.log(res)
  
            const canvas = res[0].node
            const ctx = canvas.getContext('2d')
  
            const dpr = wx.getSystemInfoSync().pixelRatio
            canvas.width = res[0].width * dpr
            canvas.height = res[0].height * dpr
            ctx.scale(dpr, dpr)
  
            //圆心坐标
            var centerX = (res[0].width/2)
            var centerY = (res[0].height/2)
            console.log(centerX);
            that.setData({
                centerX:centerX,
                centerY:centerY
            });
            //半径
            var radius = (res[0].width/2)

            ctx.beginPath();
            ctx.lineWidth = 20;
            ctx.strokeStyle = '#df1e15';
            ctx.arc(centerX,centerY,radius-25, 0, 2 * Math.PI)
            ctx.stroke();

            ctx.beginPath();
            ctx.lineWidth = 15;
            ctx.strokeStyle = '#f4ad26';
            ctx.arc(centerX,centerY,radius-40, 0, 2 * Math.PI)
            ctx.stroke();
            //画点
            var startAngel = 2 * Math.PI / 360 * (-90)  
            var dotColor =  ['#fbf0a9', '#fbb936'];
            var now=radius-30;
            for (var i = 0; i < 12; i++) {
                // 装饰点 圆心 坐标计算
                ctx.beginPath();
               var sin = now * Math.sin(startAngel);
               var cos = now * Math.cos(startAngel);
                var xr = centerX + now * Math.sin(startAngel)
                var yr = centerY - now * Math.cos(startAngel)
                
                console.log(startAngel);
                console.log(xr+'=>'+yr);
                console.log(centerX + 'center' + centerY)
                var n=i%2;
                ctx.fillStyle = dotColor[n]
                ctx.arc(xr, yr, 10, 0, 2 * Math.PI)
                ctx.fill()

                startAngel += (2 * Math.PI / 360) * (360 / 12);

            }
            //奖品数量
            var num = that.data.prizeList.length;
            console.log(num)
            // 扇形旋转转角度
            var rotateDeg = 360 / num / 2 + 90
            //绘制扇形
            var startAngel = 2 * Math.PI / 360 * (-90);
            var now=radius-100;  
            for(var i = 0; i < num; i++) {
              // 保存当前状态
              ctx.save();
              // 开始一条新路径
              ctx.beginPath();
              // 位移到圆心，下面需要围绕圆心旋转
              ctx.translate(centerX, centerY);
  
              // 从(0, 0)坐标开始定义一条新的子路径
              ctx.moveTo(0, 0);
              // 旋转弧度,需将角度转换为弧度,使用 degrees * Math.PI/180 公式进行计算。
              ctx.rotate((360 / num * i - rotateDeg) * Math.PI / 180);
              // 绘制圆弧
              ctx.arc(0, 0, radius-46, 0, 2 * Math.PI / num, false);
              var k = i%2
              var colors = ['#ffb933', '#ffe8b5']
              var color = colors[k]
              ctx.fillStyle = color
              // 填充扇形
              ctx.fill();
              // 绘制边框
             //ctx.lineWidth = 0.5;
              //ctx.strokeStyle = '#e4370e';
              //ctx.stroke();
  
              // 恢复前一个状态
              //;
     
              ctx.restore();
            }
        
            //转换画布内容为base64图片
            var dataURL=canvas.toDataURL('image/png', 1);
            that.setData({
              secs: dataURL
            })
          })
      },
  
      start: function(event) {
        //this.goto(0,60)
       var  data=this.data;
       let that=this;
       var userInfo=wx.getStorageSync('userInfo');
       wx.request({
        url: app.globalData.domain+'lottery/lottery',
        data:{from:1,user_id:userInfo.id,type:data.type,parent_id:data.parent_id},
        success:res=>{
            //console.log(res);
            if(res.statusCode==200){
                var data=res.data;
                var code=data.code;
                if(code==200){
                    var turn=data.turn;
                    that.setData({
                        k:data.id
                    })
                    that.goto(0,turn);
                }else{
                    that.showModels(code)
                }
          
            }
        }
    })
      },
        showModels:function(code){
            let that=this;
            console.log(code)
            var m=this.data.error[code]
            wx.showModal({
              title:'系统提示',
              content:''+m,
              success:res=>{
                if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
              }
            })
        },
      goBack(){
            wx.navigateBack();
          },
      goto: function(index,turn) {
        var isTurning = this.data.isTurning
        if(isTurning) {
          return
        }
        this.setData({
          transform: "rotate(" + 0 + "deg); transition: transform 5s ease-in-out"
        })
        this.setData({
          isTurning: true
        })
        this.setData({
          runRotate:0
        })
        var prizeNum = this.data.prizeList.length
        var rotate = 360/prizeNum * (prizeNum-index)
        var sRotate = 360*10+rotate
        var sRotate=sRotate-turn;
        this.setData({
          transform: "rotate(" + sRotate + "deg); transition: transform 5s ease-in-out"
        })
      },
  
      transitionEnd: function(event) {
        //这里转盘停止直接就重置转盘了,实际应该是先弹窗提示用户中奖与否,在用户关闭弹窗时,再重置转盘
        
        var k=this.data.k;
        console.log(k);
        var prizeList=this.data.prizeList;
        var title=prizeList[k].name;
        wx.showModal({
          title: '系统提示',
          content:'恭喜您抽中'+title,
          success:res=>{

          }
        })
        
      },
      onShareTimeline(){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var userInfo=wx.getStorageSync('userInfo');

        var parent_id=userInfo.id;
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         return{
           title: title,                 
           query: 'parent_id='+parent_id,        
           imageUrl:shareLogo      
           }
       },
})